#include <map>
#include <string>
#include <QString>
#include <QPushButton>
#include <QFile>
#include <QChart>
#include <QChartView>
#include "QLogTool.h"

class StyleSheetStub
{
public:
    enum BtStyleDefine {
        BtNRdy,
        BtDefault,
        BtProcing,
        BtReady,
        BtDisable,
    };
    static void CfgBtStyle(QPushButton* bt, const BtStyleDefine styleDefine)
    {
        CfgBtStyle(bt, styleDefine, bt->text());
    }

    static void CfgBtStyle(QPushButton* bt, const BtStyleDefine styleDefine, QString text)
    {
        const std::map<BtStyleDefine, QString> ButtonStyleMap {
            {BtNRdy,    "border:1px solid #242424; color:#C0C0C0;"},
            {BtDefault, "border:1px solid #242424; color:#DCDCDC;"},
            {BtProcing, "border:2px solid #C2AF11; color:#C2AF11;"},
            {BtReady,   "border:1px solid #44D52C; color:#A1FB8E;"},
            {BtDisable, "border:1px solid #242424; color:#DCDCDC;"},
        };
        if (!text.isEmpty()) {
            bt->setFont(QFont("Microsoft Yahei", 10));
            bt->setText(text);
        }
        bt->setEnabled(styleDefine != BtNRdy);
        auto itr = ButtonStyleMap.find(styleDefine);
        if (itr != ButtonStyleMap.end()) {
            bt->setStyleSheet(itr->second);
        }
    }

    static void LoadStyleSheet(QWidget* widget, QString qssPath = "D:/LibKz/design/KzStyle.css")
    {
        LOG("Loading styleSheet: " + qssPath);
        QFile file(qssPath);
        file.open(QFile::ReadOnly);
        if (!file.isOpen()) {
            LOG("styleSheet Open Failed.");
            return;
        }
        QString qss = QLatin1String(file.readAll());
        file.close();
        QString paletteColor = qss.mid(20, 7);
        widget->setPalette(QPalette(QColor(paletteColor)));
        widget->setStyleSheet(qss);
    }

    static void CfgChartStyle(QChart* pChart)
    {
        if (pChart == nullptr) {
            return;
        }
        pChart->createDefaultAxes();
        pChart->setMargins(QMargins(0, 0, 0, 0));//设置内边界全部为0
        pChart->setBackgroundRoundness(0);//设置背景区域无圆角
        pChart->setBackgroundVisible(false);
        pChart->legend()->setLabelColor(QColor(0xdc,0xdc,0xdc));
        pChart->legend()->setVisible(false);
        pChart->axes(Qt::Horizontal).back()->setLabelsColor(QColor(0xdc,0xdc,0xdc));
        pChart->axes(Qt::Vertical).back()->setLabelsColor(QColor(0xdc,0xdc,0xdc));
    }

    static void CfgChartRange( QChart* pChart,
                                   const QVariant &xMin, const QVariant &xMax,
                                   const QVariant &yMin, const QVariant &yMax)
    {
        if (pChart == nullptr) {
            return;
        }
        pChart->axes(Qt::Horizontal).back()->setRange(xMin, xMax);
        pChart->axes(Qt::Vertical).back()->setRange(yMin, yMax);
    }
};
